﻿namespace EyeOpen.Data.Test
{
	using System;
	using System.Linq;
	using EyeOpen.Domain;
	using Microsoft.VisualStudio.TestTools.UnitTesting;

	[TestClass]
	public class WebSiteDbContextTest 
		: DbContextTest<WebSiteDbContext, WebSiteDbPopulator>
	{
		[TestMethod]
		public void AddEntitytAndCheckPrimaryKeyIsAssignedByCode()
		{
			var id = Guid.NewGuid();

			using (var uow = CreateUow())
			{
				uow.LanguageRepository.Add(new Language { Id = id, Name = "English", TwoLetter = "en" });
				uow.SaveChanges();
			}

			using (var uow = CreateUow())
			{
				Assert.IsNotNull(uow.LanguageRepository.Find(id));
			}
		}

		[TestMethod]
		public void AddUserAndGet()
		{
			const string Email = "test@easycms.eye";
			const string Password = "admin";

			using (var uow = CreateUow())
			{
				uow.UserRepository.Add(new User { Id = Guid.NewGuid(), Email = Email, Password = User.CreatePassword(Password) });
				uow.SaveChanges();
			}

			using (var uow = CreateUow())
			{
				var password = User.CreatePassword(Password);

				var user =
					uow
						.UserRepository
						.SingleOrDefault(x => x.Email == Email && x.Password == password);

				Assert.IsNotNull(user);
			}
		}
	}
}